package com.atguigu.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.atguigu.edu.entity.Subject;
import com.atguigu.edu.entity.excel.SubjectData;
import com.atguigu.edu.entity.subject.OneSubject;
import com.atguigu.edu.entity.subject.TwoSubject;
import com.atguigu.edu.listener.SubjectExcelListener;
import com.atguigu.edu.mapper.SubjectMapper;
import com.atguigu.edu.service.SubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-02
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    /**
     * 添加课程分类
     *
     * @param file
     * @param subjectService
     */
    @Override
    public void saveSubject(MultipartFile file, SubjectService subjectService) {
        try {
            //获取文件的输入流
            InputStream inputStream = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
        }
    }

    /**
     * 获取 树形结构的 课程分类列表
     *
     * @return
     */
    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        //1.查询出所有的一级分类 parent_id = 0
        QueryWrapper<Subject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<Subject> oneSubjectList = baseMapper.selectList(wrapperOne);
//      List<Subject> list = this.list(wrapper);

        //2.查询出所有的二级分类
        QueryWrapper<Subject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0"); //ne 不等于 eq 等于
        List<Subject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建list集合，用于存储最终封装的分类数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //3.封装一级分类
        //查询出来所有的一级分类list集合，遍历得到每一个分类对象，获取每一个一级分类对象值
        //封装到要求的list集合里面去 finalSubjectList
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //得到每一个subject对象
            Subject subject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setTitle(subject.getTitle());
//            oneSubject.setId(subject.getId());
            BeanUtils.copyProperties(subject, oneSubject); //第一个 源对象 第二个 目标对象
            finalSubjectList.add(oneSubject);

            //4.封装二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int j = 0; j < twoSubjectList.size(); j++) {
                Subject subjectTwo = twoSubjectList.get(j);
                //判断二级分类的id是否和 一级分类的id一样
                if (subjectTwo.getParentId().equals(subject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subjectTwo, twoSubject);

                    twoFinalSubjectList.add(twoSubject);
                }
            }

//            twoSubjectList.forEach(twoSubject ->{
//                if(twoSubject.getParentId().equals(subject.getId())){
//                    TwoSubject twoSubject1 = new TwoSubject();
//                    BeanUtils.copyProperties(twoSubject,twoSubject1);
//                    twoFinalSubjectList.add(twoSubject1);
//                }
//            });
            //把一级下面的 所有二级分类 放到一级分类里面
            oneSubject.setChildren(twoFinalSubjectList);

        }

        return finalSubjectList;
    }
}
